<?php
namespace Home\Controller;
use Home\Common\Common;
class AtrecordinfoController extends Common {

    public function index(){
        $atrtypeinfo = M('atrtypeinfo');
        $list = $atrtypeinfo->select();
        $this->assign('list',$list);
        $this->display();
    }

   public  function add(){
       if (empty($_POST['submit'])){
           $this->display();
       }else{
           if (empty($_POST['atrnmae'])){
               $this->error('名称不能为空',U('index'));
           }
           $atrtypeinfo = M('atrtypeinfo');
           $atrtypeinfo->add($_POST);
           $this->success('添加成功',U('index'));
       }
    }

    public function edit(){
        $approvalType = M('atrtypeinfo');
        if (empty($_POST['submit'])){
            if (empty($_GET['atrid'])){
                $this->error('参数错误',U('index'));
            }
            $info = $approvalType->where('`atrid`='.$_GET['atrid'])->find();
            $this->assign('info',$info);
            $this->display();
        }else{
            if (!empty($_POST['atrnmae'])){
                $approvalType->where('`atrid`='.$_POST['atrid'])->save($_POST);
                $this->success('修改成功',U('index'));
            }
        }
    }
    public function set(){
        $setinfo=M('setinfo');
        if(empty($_POST['hidden'])){
        if(empty($_GET['atrid'])){
                $this->error('参数错误',U('index'));
            }
            $list = $setinfo->where('`artid`='.$_GET['atrid'].' and `states`=1')->order('sort')->select();
            $postinfo=M('postinfo');
            $postinfoNameList = $postinfo->where('`state`=1')->getField('pt_id,pt_name');
            $postinfoNameList[0] = '董事长';
            $postinfoList = $postinfo->where('state=1')->select();//查询已经启用的岗位
            $postinfoList[] = array('pt_id'=>0,'pt_name'=>'董事长','status'=>1);
            $postinfoSetList = $setinfo->where('`states`=1 and `artid`='.$_GET['atrid'])->getField('pt_id,artid');
            $this->assign('postinfoNameList',$postinfoNameList);
            $this->assign('postinfoList',$postinfoList);
            $this->assign('postinfoSetList',$postinfoSetList);
            $this->assign('list',$list);
            $this->display();
        }else{
            $setinfo->where('`artid`='.$_POST['atrid'])->save(array('states'=>0));
            foreach ($_POST['atrids'] as $k=>$rid){
                if ($setinfo->where('`artid`='.$_POST['atrid'].' and `pt_id`='.$rid)->find()){
                    $setinfo->where('`artid`='.$_POST['atrid'].' and `pt_id`='.$rid)->save(array('states'=>1,'sort'=>($k+1)));
                }else {
                    $setinfo->add(array('artid' => $_POST['atrid'], 'pt_id' => $rid, 'sort' => ($k+1), 'states' => 1));
                }
            }
            $this->success('设置成功',U('index'));
        }
    }


    function check_list(){
        $applicationinfo = M('applicationinfo');
        $setinfo = M('setinfo');
        $list = [];
        $staffinfoNameList = M('staffinfo')->getField('stf_id,stf_name,pt_id');
        $postinfoNameList = M('postinfo')->getField('pt_id,pt_name');
        $atList = M('atrtypeinfo')->getField('atrid,atrnmae');

        $aSet = $setinfo->where('`pt_id`='. $this->_listprid['pt_id'].' and `states`=1')->select();

        if(empty($this->_listprid['dpt_id'])){
            $lprid='';
        }else{
            $lprid='`dpt_id`='.$this->_listprid['dpt_id'].' and ';
        }


        foreach ($aSet as $as){
            $_list = $applicationinfo->where($lprid.'`at_type`='.$as['artid'].' and `status`='.$as['sort'])->select();
            if (!empty($_list)){
                $list[] = $_list;
            }
        }
        $this->assign('atList',$atList);
        $this->assign('list',$list);
        $this->assign('staffinfoNameList',$staffinfoNameList);
        $this->assign('postinfoNameList',$postinfoNameList);
        $this->display();
    }

    function a_show(){
        $approval = M('applicationinfo');//申请表
        $info = $approval->where('`at_id`='.$_GET['at_id'])->find();
        $atList = M('atrtypeinfo')->getField('atrid,atrnmae');//类型表
        $info['type'] = $atList[$info['at_type']];
        $logList = M('at_recordinfo')->where('`at_id`='.$_GET['at_id'])->select();//记录表
        $adminList = M('staffinfo')->getField('stf_id,stf_name');//员工表

        $this->assign('adminList',$adminList);
        $this->assign('logList',$logList);
        $this->assign('info',$info);
        $this->display();
    }
    function a_shows(){
        $approval = M('recordinfo');//调遣申请表
        $info = $approval->where('`rmd_id`='.$_GET['rmd_id'])->find();
        $atList = M('atrtypeinfo')->getField('atrid,atrnmae');
        $info['type'] = $atList[$info['at_type']];
        $logList = M('at_recordinfo')->where('`at_id`='.$_GET['rmd_id'])->select();
        $adminList = M('staffinfo')->getField('stf_id,stf_name');

        $this->assign('adminList',$adminList);
        $this->assign('logList',$logList);
        $this->assign('info',$info);
        $this->display();
    }

    function a_check(){
        if (!empty($_POST['submit'])){
            $applicationinfo = M('recordinfo');
            $setinfo = M('setinfo');
            $info = $applicationinfo->where('`rmd_id`='.$_POST['rmd_id'])->find();
            $asList = $setinfo->where('`artid`='.$info['at_type'].' and `states`=1')->count();
            $at_recordinfo = M('at_recordinfo');
            $data = [];
            if ($_POST['rd_result'] == 1){
                if ($info['status'] + 1 > $asList){
                    $data['status'] = 0;
                }else {
                    $data['status'] = $info['status'] + 1;
                }
            }else{
                $data['status'] = -1;
            }
            $_POST['rd_man']=$_SESSION['aid'];
            $_POST['rd_time']=date('Y-m-d H:i:s');
            $at_recordinfo->add($_POST);
            $applicationinfo->where('`rmd_id`='.$_POST['rmd_id'])->save($data);

            $this->success('审核成功',U('check_list'));
        }
    }

    function a_checks(){
        if (!empty($_POST['submit'])){
            $applicationinfo = M('recordinfo');
            $setinfo = M('setinfo');
            $info = $applicationinfo->where('`rmd_id`='.$_POST['rmd_id'])->find();
            $asList = $setinfo->where('`artid`='.$info['at_type'].' and `states`=1')->count();
            $at_recordinfo = M('at_recordinfo');
            $data = [];
            if ($_POST['rd_result'] == 1){
                if ($info['status'] + 1 > $asList){
                    $data['status'] = 0;
                }else {
                    $data['status'] = $info['status'] + 1;
                }
            }else{
                $data['status'] = -1;
            }
            $_POST['rd_man']=$_SESSION['aid'];
            $_POST['rd_time']=date('Y-m-d H:i:s');
            $at_recordinfo->add($_POST);
            $applicationinfo->where('`rmd_id`='.$_POST['rmd_id'])->save($data);
            $this->success('审核成功',U('dg_sp'));
        }
    }


    public function  del(){
        if(empty($_GET['atrid'])){
            $this->error('参数错误');
        }
        else{
            $cont = M('atrtypeinfo');
            $cont = $cont->where(array('atrid'=>$_GET['atrid']))->delete();
            if($cont){
                $this->success('删除成功');
            }else{
                $this->success('删除失败，请重新尝试');
            }
        }
    }

    public function dg_sp()
    {
        $recordinfo = M('recordinfo');//调遣信息表
        $setinfo = M('setinfo');//流程设置表
        $list = [];
        $staffinfoNameList = M('staffinfo')->getField('stf_id,stf_name,pt_id');//员工表
        $departmentinfo=M('departmentinfo')->getField('dpt_id,dpt_name');//部门表
        $postinfoNameList = M('postinfo')->getField('pt_id,pt_name');//岗位表
        $atList = M('atrtypeinfo')->getField('atrid,atrnmae');//流程表

        $aSet = $setinfo->where('`pt_id`=' . $this->_listprid['pt_id'] . ' and `states`=1')->select();

            foreach ($aSet as $as) {
                $_list = $recordinfo->where('`at_type`=' . $as['artid'] . ' and `status`=' . $as['sort'])->select();
                if (!empty($_list)) {
                    $list[] = $_list;
                }
            }
        $this->assign('atList', $atList);
        $this->assign('list', $list);
        $this->assign('departlist', $departmentinfo);
        $this->assign('staffinfoNameList', $staffinfoNameList);
        $this->assign('postinfoNameList', $postinfoNameList);
        $this->display();
    }

    public function dg_spp()
    {
        $recordinfo = M('recordinfo');//调遣信息表
        $setinfo = M('setinfo');//流程设置表
        $list = [];
        $staffinfoNameList = M('staffinfo')->getField('stf_id,stf_name,pt_id');//员工表
        $departmentinfo=M('departmentinfo')->getField('dpt_id,dpt_name');//部门表
        $postinfoNameList = M('postinfo')->getField('pt_id,pt_name');//岗位表
        $atList = M('atrtypeinfo')->getField('atrid,atrnmae');//流程表

        $aSet = $setinfo->where('`pt_id`=' . $this->_listprid['pt_id'] . ' and `states`=1')->select();

        foreach ($aSet as $as) {
            $_list = $recordinfo->where('`at_type`=' . $as['artid'])->select();
            if (!empty($_list)) {
                $list[] = $_list;
            }
        }
        $this->assign('atList', $atList);
        $this->assign('list', $list);
        $this->assign('departlist', $departmentinfo);
        $this->assign('staffinfoNameList', $staffinfoNameList);
        $this->assign('postinfoNameList', $postinfoNameList);
        $this->display();
    }
}